package com.example.admin.yuejian21.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.admin.yuejian21.Model.TrainModel;
import com.example.admin.yuejian21.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class PlaceOrderActivity extends AppCompatActivity {
    private Button train_buy;
    private ImageButton btn_add,btn_less;
    private ImageView train_back;
    private EditText placeordername,placeorderphone,placeorderprice,placeordernotice;
    private TextView placeordernum,pay;
    private String train_id;
    private String str_name;
    private String str_tel;
    private String str_buynum;
    private String str_paybotice,str_pay;
    private Double price,allprice,sum;
    private int user_id;
    private int num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_place_order);
        initViews();
        initEvents();
    }
    private void initViews(){
        placeordername= (EditText) findViewById(R.id.placeorder_name);
        placeorderphone= (EditText) findViewById(R.id.train_phone);
        placeordernum = (TextView) findViewById(R.id.placeorder_num);
        placeorderprice = (EditText) findViewById(R.id.placeorder_price);
        placeordernotice = (EditText) findViewById(R.id.notice);
        train_back = (ImageView) findViewById(R.id.train_back);
        train_buy = (Button) findViewById(R.id.train_buy);
        btn_add = (ImageButton) findViewById(R.id.btn_add);
        btn_less = (ImageButton) findViewById(R.id.btn_less);
        pay = (TextView) findViewById(R.id.pay);
        Intent intent = getIntent();
        train_id = intent.getStringExtra("train_id");
        price = Double.valueOf(intent.getStringExtra("price"));
        user_id = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getInt("user_id",0);
        str_buynum = placeordernum.getText().toString();
        Double buynum = Double.parseDouble(str_buynum);
        allprice = buynum*price;
        pay.setText(allprice.toString());
    }
    private void initEvents() {
        train_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        train_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buy();
            }
        });
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (num>0){
                    num++;
                }
                placeordernum.setText(Integer.toString(num));
                String foodnum = placeordernum.getText().toString();
                num = Integer.parseInt(foodnum);
                Double numa = Double.parseDouble(foodnum);
                sum = numa*allprice;
                placeorderprice.setText(sum.toString());
            }
        });
        btn_less.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (num>1){
                    num--;
                }else{
                    Toast.makeText(PlaceOrderActivity.this,"购买数量不能为负！",Toast.LENGTH_SHORT).show();
                }
                placeordernum.setText(Integer.toString(num));
                String foodnum = placeordernum.getText().toString();
                num = Integer.parseInt(foodnum);
                Double numb = Double.parseDouble(foodnum);
                sum = numb*allprice;
                placeorderprice.setText(sum.toString());
            }
        });
    }
    private void buy(){
        str_name = placeordername.getText().toString();
        str_tel = placeorderphone.getText().toString();
        str_paybotice = placeordernotice.getText().toString();
        str_buynum = placeordernum.getText().toString();
        str_pay=allprice.toString();
        TrainModel trainModel = new TrainModel();
        Call<Integer> call = trainModel.signTrain(str_name,train_id,str_tel,str_buynum,"1",str_pay,str_paybotice,user_id);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body()!=0){
                    Toast.makeText(PlaceOrderActivity.this,"下单成功",Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(PlaceOrderActivity.this,"下单失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                Toast.makeText(PlaceOrderActivity.this,"获取连接失败",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

